﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GrewalCRM.Entities.Common;
using GrewalCRM.Entities;
using GrewalCRM.DAL;

namespace GrewalCRM.BLL
{
  public class InstallersMaster_BLL : BaseBLLGrewalCRM
  {
    public InstallersMaster_BLL()
    {
      baseDAL = new InstallersMaster_DAL();
    }

    #region Public Methods
    public List<InstallersMaster> GetAllInstaller()
    {
      List<BaseEntitiesGrewalCRM> baseEnttities = this.GetAll();
      List<InstallersMaster> installers = new List<InstallersMaster>();

      if (baseEnttities != null && baseEnttities.Count > 0)
      {
        foreach (BaseEntitiesGrewalCRM baseEntity in baseEnttities)
        {
          installers.Add(baseEntity as InstallersMaster);
        }
      }

      return installers;
    }

    public InstallersMaster GetInstallerByID(int InstallerID)
    {
        BaseEntitiesGrewalCRM baseEntity = this.GetByID(InstallerID);
       
        if (baseEntity != null)        
            return baseEntity as InstallersMaster;            
        else
        return null;
    }

    public void FillInstaller(InstallersMaster Entity)
    {
        FillEntity(Entity);
        
    }

    public int Save(InstallersMaster Entity)
    {
        int result = 0;
        EntityState state = EntityState.Nutral;

        if (Entity.ID == 0)
            state = EntityState.New;
        else if (Entity.IsMarkedDeleted)
            state = EntityState.Deleted;
        else if (Entity.IsChanged)
            state = EntityState.Changed;

        switch (state)
        {
            case EntityState.New:
                result = AddNew(Entity);
                break;
            case EntityState.Deleted:
                result = Delete(Entity);
                break;
            case EntityState.Changed:
                result = Update(Entity);
                break;
            default:
                result = 0;
                break;

        }

        return result;

    }
    #endregion

    #region Protected  Methods
   

    protected override int AddNew(BaseEntitiesGrewalCRM Entity)
    {
      return baseDAL.AddNew(Entity);
    }

    protected override int Update(BaseEntitiesGrewalCRM Entity)
    {
      return baseDAL.Update(Entity);
    }

    protected override int Delete(BaseEntitiesGrewalCRM Entity)
    {
      return baseDAL.Delete(Entity);
    } 

    protected override List<BaseEntitiesGrewalCRM> GetAll()
    {
      return baseDAL.GetAll();
    }

    protected override BaseEntitiesGrewalCRM GetByID(int ID)
    {
        return baseDAL.GetByID(ID);
    }

    protected override void FillEntity(BaseEntitiesGrewalCRM Entity)
    {
        InstallersMaster tempInstaller = (baseDAL.GetByID(Entity.ID)) as InstallersMaster;

        InstallersMaster sourceInstaller = Entity as InstallersMaster;


        if(tempInstaller != null && sourceInstaller != null)
        {
            sourceInstaller.Address = tempInstaller.Address;
            sourceInstaller.ID = tempInstaller.ID;
            sourceInstaller.InstallerCommission = tempInstaller.InstallerCommission;
            sourceInstaller.InstallerName = tempInstaller.InstallerName;
            sourceInstaller.IsActive = tempInstaller.IsActive;
            sourceInstaller.LastModifiedDateTime = tempInstaller.LastModifiedDateTime;
            sourceInstaller.LastModifiedUser = tempInstaller.LastModifiedUser;
            sourceInstaller.MobileNo = tempInstaller.MobileNo;
            sourceInstaller.PhoneNo = tempInstaller.PhoneNo;
        }
    } 
    #endregion
  }
}
